﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
	private string cs = System.Configuration.ConfigurationManager.ConnectionStrings["Resist"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
		int PageNum = 0, qty = 10;
		try { PageNum = int.Parse(Request.QueryString["Page"]); } catch {}
		try { qty = int.Parse(Request.QueryString["qty"]); } catch { }
		LoadPosts(qty, PageNum);
    }
	protected void rPosts_ItemDataBound(object sender, RepeaterItemEventArgs e)
	{
		if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
		DataRow dr = (DataRow)e.Item.DataItem;
		((HyperLink)e.Item.FindControl("hlTitle")).Text = Server.HtmlEncode(dr["Title"].ToString());
		((HyperLink)e.Item.FindControl("hlTitle")).NavigateUrl = "http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString();
		((Label)e.Item.FindControl("lblDate")).Text = ((DateTime)dr["Created"]).ToString("MM/dd/yyyy hh:mm ttt");
		string body = common.StripTagsCharArray(dr["Body"].ToString());
		if (body.Contains("!break")) body = body.Substring(0, body.IndexOf("!break")) + "<a href=\"http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString() + "\" alt=\"Continue Reading \"> [...] </a>";
		else if (body.Length > 1024) body = body.Substring(0, 1000) + "<a href=\"http://www.ResistOccupation.com/Post.aspx?" + dr["FriendlyName"].ToString() + "\" alt=\"Continue Reading \"> [...] </a>";
		((Label)e.Item.FindControl("lblBody")).Text = body;
		Image img = (Image)e.Item.FindControl("img");
		if (dr["Image"].GetType() != typeof(DBNull)) {
			img.Visible = true;
			img.ImageUrl = "Image.aspx?" + dr["ID"].ToString(); }

		DataTable dtCats = KC.DataAccess.Global.Global.CallSql_Query(cs, "select * from Categories where ID in (select CategoryID from PostCategoryMap where PostID=" + dr["ID"].ToString() + ") order by Name");
		string cats = "";
		foreach (DataRow cat in dtCats.Rows)
			cats += "<a href=\"Categories.aspx?" + cat["FriendlyName"].ToString() + "\" Title=\"" + Server.HtmlEncode(cat["Description"].ToString()) + "\">" + cat["Name"].ToString() + "</a>, ";

		((Label)e.Item.FindControl("lblCats")).Text = cats.TrimEnd(new char[] { ',', ' ' });
 	}
	private void LoadPosts(int qty, int PageNum)
	{
		int skip = (qty * PageNum + 1);
		DataTable dt = KC.DataAccess.Global.Global.CallSql_Query(cs, "with Posts2 as (select *, row_number() over (order by id desc) as RowNumber from Posts) " +
			"select top " + qty.ToString() + " * from Posts2 where RowNumber between " + skip.ToString() + " and " + (skip + 10).ToString() + " and isenabled=1");
		rPosts1.DataSource = dt.Select().Take(dt.Rows.Count / 2);
		rPosts1.DataBind();
		rPosts2.DataSource = dt.Select().Skip(dt.Rows.Count / 2);
		rPosts2.DataBind();
	}


}